using OpenSource.SpreadsheetFactory.Internal;
using System;
using System.Collections.Generic;
using System.IO;

namespace OpenSource.SpreadsheetFactory
{
    /// <summary>
    /// A factory class to create spreadsheets
    /// </summary>
    [CLSCompliant(true)]
    public static class CreateSpreadsheet
    {
        /// <summary>
        /// Generates a spreadsheet and returns it as a byte array
        /// </summary>
        /// <typeparam name="T">The type of the records of the spreadsheet.</typeparam>
        /// <param name="source">The collection of records to be exported.</param>
        /// <param name="spreadsheetType">The type of the spreadsheet.</param>
        /// <returns>A byte array representation of the spreadsheet document.</returns>
        public static byte[] GetSpreadsheetBytes<T>(IEnumerable<T> source, SpreadsheetType spreadsheetType)
        {
            return SpreadsheetGenerator<T>.CreateSpreadsheetGenerator(source, spreadsheetType).GenerateSpreadsheet();
        }

        /// <summary>
        /// Generates a spreadsheet and saves it as a file.
        /// </summary>
        /// <typeparam name="T">The type of the records of the spreadsheet</typeparam>
        /// <param name="source">The collection of records to be exported.</param>
        /// <param name="spreadsheetType">The type of the spreadsheet.</param>
        /// <param name="path">The path of the file the spreadsheet will be saved to.</param>
        public static void SaveSpreadsheetAsFile<T>(IEnumerable<T> source, SpreadsheetType spreadsheetType, string path)
        {
            File.WriteAllBytes(path, GetSpreadsheetBytes<T>(source, spreadsheetType));
        }
    }
}

